package com.example.finalproject.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.finalproject.MainActivity;
import com.example.finalproject.R;
import com.example.finalproject.bean.LoginBean;
import com.example.finalproject.utils.DatabaseHelper;

public class RegisterActivity extends AppCompatActivity {
    private EditText editUsernameRegister, editPasswordRegister;
    private Button buttonRegisterPage;
    private DatabaseHelper databaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        databaseHelper = DatabaseHelper.getInstance(this);
        editUsernameRegister = findViewById(R.id.editUsernameRegister);
        editPasswordRegister = findViewById(R.id.editPasswordRegister);
        buttonRegisterPage = findViewById(R.id.buttonRegisterPage);

        buttonRegisterPage.setOnClickListener(view -> {
            String name = editUsernameRegister.getText().toString();
            String pass = editPasswordRegister.getText().toString();
            LoginBean loginBean = new LoginBean(name, pass);
            if (databaseHelper.register(loginBean) != -1) {
                Toast.makeText(this, "注册成功！", Toast.LENGTH_SHORT).show();
                Intent intentAccount = new Intent(this, MainActivity.class);
                intentAccount.putExtra("navigate_to_index", 2);
                startActivity(intentAccount);
                finish();
            } else {
                Toast.makeText(this, "注册失败！", Toast.LENGTH_SHORT).show();
            }
        });


    }
}